package com.cyan.app.workflow.widget.PullToRefresh.internal;

import android.annotation.TargetApi;
import android.content.Context;
import android.graphics.drawable.AnimationDrawable;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import com.cyan.app.workflow.R;


public class LoadingLayout extends FrameLayout {
	static final int DEFAULT_ROTATION_ANIMATION_DURATION = 150;

	private final ImageView headerImage;
	private final TextView headerText;
	private final TextView mRefreshLastTime;

	private String pullLabel;
	private String refreshingLabel;
	private String releaseLabel;
	private AnimationDrawable mCoinRoationDrawable = null;
	
	private int[] coinDrawableIds = new int[]{R.drawable.coin_1, R.drawable.coin_2, R.drawable.coin_3, R.drawable.coin_4,
									R.drawable.coin_5, R.drawable.coin_6, R.drawable.coin_7, R.drawable.coin_8};
	
//	private final Animation rotateAnimation, resetRotateAnimation;

	public LoadingLayout(Context context, final int mode, String releaseLabel, String pullLabel, String refreshingLabel) {
		super(context);
		ViewGroup header = (ViewGroup) LayoutInflater.from(context).inflate(R.layout.pull_to_refresh_header, this);
		headerText = (TextView) header.findViewById(R.id.pull_to_refresh_text);
		headerImage = (ImageView) header.findViewById(R.id.pull_to_refresh_image);
		mRefreshLastTime = (TextView) header.findViewById(R.id.pull_to_refresh_lasttime);
		mCoinRoationDrawable = (AnimationDrawable) getContext().getResources().getDrawable(R.drawable.coin_animation);

		this.releaseLabel = releaseLabel;
		this.pullLabel = pullLabel;
		this.refreshingLabel = refreshingLabel;
	}

	public void reset() {
		headerText.setText("刷新完成");
		headerImage.setVisibility(View.VISIBLE);
		mCoinRoationDrawable.stop();
		setViewBackground(headerImage,getContext().getResources().getDrawable(coinDrawableIds[0]));
	}

	public void releaseToRefresh() {
		headerText.setText(releaseLabel);
	}

	public void setPullLabel(String pullLabel) {
		this.pullLabel = pullLabel;
	}

	public void refreshing() {
		headerText.setText(refreshingLabel);
		setViewBackground(headerImage, mCoinRoationDrawable);
		mCoinRoationDrawable.start();
	}

	@TargetApi(Build.VERSION_CODES.JELLY_BEAN)
	private void setViewBackground(View imageView,Drawable drawable){
		if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN){
			imageView.setBackground(drawable);
		}else{
			imageView.setBackgroundDrawable(drawable);
		}
	}
	
	public void setRefreshingLabel(String refreshingLabel) {
		this.refreshingLabel = refreshingLabel;
	}

	public void setReleaseLabel(String releaseLabel) {
		this.releaseLabel = releaseLabel;
	}

	public void updateImageState(float progress){
		int drawableId = ((int)(progress * coinDrawableIds.length)) % coinDrawableIds.length;
		setViewBackground(headerImage,getContext().getResources().getDrawable(coinDrawableIds[drawableId]));
	}
	
	public void pullToRefresh() {
		headerText.setText(pullLabel);
//		headerImage.clearAnimation();
//		headerImage.startAnimation(resetRotateAnimation);
	}

	public void setTextColor(int color) {
		headerText.setTextColor(color);
	}

	public void setLastTimeShow(boolean show) {
		mRefreshLastTime.setVisibility(show ? View.VISIBLE : View.GONE);
	}
	 
	public void setLastTimeDes(String lastTimeDes) {
		mRefreshLastTime.setText(lastTimeDes);
	}
}
